<?php include '../common/session.php' ?>
<?php include '../common/header.php' ?>
<?php
	if( $_SESSION['userType'] != 'all' ){
		$url = $_SERVER['HTTP_REFERER'];
		header('location:'.$url);
	}
	$active = $_GET['active'];
	$id = $_GET['id'];
	
	if( $active == 'edit' ){
		include "../common/connect.php";
		$result = mysql_query("SELECT * FROM user WHERE id='".$id."'");
		mysql_close($con);
		$row = mysql_fetch_array($result);
		if( !is_array($row) ){
			$url = $_SERVER['HTTP_REFERER'];
			header('location:'.$url);
		}
	}
?>
<body>
<?php include "menus.php" ?>
<div id="main">
	<h2>添加管理员<a href="userAdmin.php">[返回]</a></h2>
	<div class="addUser" uid="<?php echo $id ?>">
		<label>
			<span><?php if( $active != 'edit' ){echo '新';} ?>用户名称：</span>
			<input class="txt username" type="text" value="<?php if( $active == 'edit' ){ echo $row['username']; } ?>" />
		</label>
		<label>
			<span><?php if( $active != 'edit' ){echo '新';} ?>用户密码：</span>
			<input class="txt password1" type="password" value="" />
		</label>
		<label>
			<span>确认密码：</span>
			<input class="txt password2" type="password" value="" />
		</label>
		<label>
			<span><?php if( $active != 'edit' ){echo '新';} ?>用户权限：</span>
			<select>
				<option value="1" <?php if( $active == 'edit' && $row['type'] == 'all' ){ ?> selected="selected" <?php } ?> >全部开放（all）</option>
				<option value="2" <?php if( $active == 'edit' && $row['type'] == 'part' ){ ?> selected="selected" <?php } ?> >部分开放（part）</option>
				<option value="3" <?php if( $active == 'edit' && $row['type'] == 'less' ){ ?> selected="selected" <?php } ?> >少量开放（less）</option>
			</select>
		</label>
		<button>提交</button>
	</div>
</div>
</body>
<script type="text/javascript">
$(document).ready(function(){
	$('button').click(function(){
		var username = $('input.username').val(),
			password1 = $('input.password1').val(),
			password2 = $('input.password2').val(),
			type = $('select option:selected').val();
			
		if( username && password1 && password2 && type && password1 === password2 ){
			var data = {
				username : username,
				password : password1,
				type : type
			}
			Dir.ajax('addUserForm.php?id='+$('.addUser').attr('uid'),data,function(res){
				if(res.status){
					alert('提交成功');
					window.location.href = 'userAdmin.php';
				}
			});
		}else{
			alert('信息不完整或密码错误');
		}
	});
});
</script>
</html>